<?php
namespace Statistics\Controller;
use Common\Controller\AdminbaseController;
use Think\Log;

class IndexController extends AdminbaseController {
	function _initialize() {
		parent::_initialize();
	}
/*	function index(){
        $output = '';
        $return_val = 0;
        try {
            $result =  exec('cat /data/tj/* 2>&1', $output, $return_val);
        } catch (Exception $e) {
            print $e->getMessage();
            exit();
        }
        $this->assign("statistics", $output);
        $this->display();
	}*/
    /*
     * 通用统计页面
     */
    function index(){
        $count = D('statistics')
            ->where('devid IS NOT NULL AND devid!= 0')
            ->group("object_id, DATE_FORMAT(a_time, '%Y%m%d')")
            ->order("DATE_FORMAT(a_time, '%Y%m%d') desc")
            ->count();
        $page = $this->page($count, 50);

        $sql = "SELECT
                    DATE_FORMAT(ys.a_time, '%Y%m%d') as a_time,
                    ys.object_id,
                    yso.desc,
                    COUNT(ys.id) AS total_count,
                    COUNT(DISTINCT ys.userid)  AS total_user_count,
                    COUNT(DISTINCT ys.devid) AS total_dev_count
                FROM
                    `yy_statistics` ys
                    LEFT JOIN yy_statistics_object yso ON yso.obj_id = ys.object_id
                GROUP BY
                    object_id,
                    DATE_FORMAT(ys.a_time, '%Y%m%d')
                    ORDER BY a_time desc
                    LIMIT ".$page->firstRow . ',' . $page->listRows;

        $data = D('statistics')->query($sql);

        $this->assign("Page", $page->show('Admin'));
        $this->assign("statistics", $data);
        $this->display('index');
    }

    function detail() {
        $count = D('statistics')
            ->where('devid IS NOT NULL AND devid!= 0')
            ->count();
        $page = $this->page($count, 50);

        $sql = "SELECT ys.*,
                   yso.desc,
                   yru.username,
                   yru.phone,
                   yru.nickname,
                   yd.devid,
                   yd.barcode,
                   yst.title,
                   yst.address
              FROM `yy_statistics` ys
              LEFT JOIN yy_statistics_object yso ON yso.obj_id= ys.object_id
              LEFT JOIN yy_real_users yru ON ys.userid= yru.id
              LEFT JOIN yy_device yd ON(yd.devid= ys.devid
               AND ys.devid IS NOT NULL
               AND ys.devid!= 0)
              LEFT JOIN yy_store yst ON yst.store_id= yd.store_id
               LIMIT ".$page->firstRow . ',' . $page->listRows;
        $data = D('statistics')->query($sql);

        $this->assign("Page", $page->show('Admin'));
        $this->assign("statistics", $data);
        $this->display('detail');
    }
}
